#include <iostream>
#include <sstream>
#include <unordered_map>
#include <map>
#include "algorithm"
#include "vector"
using namespace std;


struct Combination{
    string name;
    string color;
    int rank;
    Combination(){}
    Combination(string n,string c,int r) : name(n),color(c),rank(r){}

};


void filter(vector<Combination>& result){
    //分别表示1，2，3，4，M，E，R，F，red,purple,blue,green出现的次数
    int num[12]={0,0,0,0,0,0,0,0,0,0,0,0};
    map<int,string> m = {
            {1,"1"},{2,"2"},{3,"3"},{4,"4"},{5,"M"},{6,"E"},
            {7,"R"},{8,"F"},{9,"red"},{10,"purple"},{11,"blue"},{12,"green"},
    };
    for (const auto& temp : result) {
        if (temp.rank==1){
            num[0]++;
        } else if (temp.rank==2){
            num[1]++;
        }else if (temp.rank==3){
            num[2]++;
        }else if (temp.rank==4){
            num[3]++;
        }
        if (temp.name=="M"){
            num[4]++;
        } else if (temp.name=="E"){
            num[5]++;
        }else if (temp.name=="R"){
            num[6]++;
        }else if (temp.name=="F"){
            num[7]++;
        }
        if (temp.color=="red"){
            num[8]++;
        } else if (temp.color=="purple"){
            num[9]++;
        }else if (temp.color=="blue"){
            num[10]++;
        }else if (temp.color=="green"){
            num[11]++;
        }
    }
    //对应排名
    for (int i = 0; i < 4; ++i) {
        //表示rank只出现一次，即该rank所属对象已经正确组合，其color和name属性均正确，其余重复的应该删除
        if (num[i]==1){
            Combination temp;
            int index;//保存temp在result中的index
            for (int j=0;j<result.size();j++) {
                if (result[j].rank==i+1){
                    temp = result[j];
                    index = j;
                    break;
                }
            }
            int count=0;
            int array[64];
            for (int j = 0; j < result.size(); ++j) {
                if (j!=index&&(result[j].name==temp.name||result[j].color==temp.color)){
                    array[count++]=j;
                }
            }
            for (int j = count-1; j >=0; --j) {
                result.erase(result.begin()+array[j]);
            }
        }
    }

    //对应姓名
    for (int i = 4; i < 8; ++i) {
        if (num[i]==1){
            Combination temp;
            int index;//保存temp在result中的index
            auto name = m.find(i+1);
            for (int j=0;j<result.size();j++) {
                if (result[j].name==name->second){
                    temp = result[j];
                    index = j;
                    break;
                }
            }
            int count=0;
            int array[64];
            for (int j = 0; j < result.size(); ++j) {
                if (j!=index&&(result[j].color==temp.color||result[j].rank==temp.rank)){
                    array[count++]=j;
                }
            }
            for (int j = count-1; j >=0; --j) {
                result.erase(result.begin()+array[j]);
            }
        }
    }
    //对应颜色
    for (int i = 8; i < 12; ++i) {
        if (num[i]==1){
            Combination temp;
            int index;//保存temp在result中的index
            auto name = m.find(i+1);
            for (int j=0;j<result.size();j++) {
                if (result[j].color==name->second){
                    temp = result[j];
                    index = j;
                    break;
                }
            }
            int count=0;
            int array[64];
            for (int j = 0; j < result.size(); ++j) {
                if (j!=index&&(result[j].name==temp.name||result[j].rank==temp.rank)){
                    array[count++]=j;
                }
            }
            for (int j = count-1; j >=0; --j) {
                result.erase(result.begin()+array[j]);
            }
        }
    }
}

vector<Combination> findResult(){
    vector<string> names = {"M","E","R","F"};
    vector<string> colors = {"red","purple","blue","green"};
    vector<int> ranks = {1,2,3,4};
    vector<Combination> result;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            for (int k = 0; k < 4; ++k) {
                Combination temp = {names[i],colors[j],ranks[k]};
                result.push_back(temp);
                //Ellie骑紫色的车
                if ((temp.name=="E"&&temp.color!="purple")||(temp.color=="purple"&&temp.name!="E")) {
                    result.pop_back();continue;
                }
                //Rohan没有骑绿色的车（且不是紫色的车子）
                if (temp.name=="R"&&(temp.color=="purple"||temp.color=="green")) {
                    result.pop_back();continue;
                }
                //Mike和Ellie不是第一，也不是最后
                if (((temp.name=="M"||temp.name=="E")&&(temp.rank==1|| temp.rank==4))||((temp.rank==2||temp.rank==3))&&(temp.name=="R"||temp.name=="F")) {
                    result.pop_back();continue;
                }
                //骑蓝色车的人领先Fatma--》F不可能第一
                if (temp.name=="F"&&temp.rank==1){
                    result.pop_back();continue;
                }
                //骑绿色车的人领先骑紫色车的人---》骑绿色车的人的rank！=4
                if (temp.color=="green"&&temp.rank==4){
                    result.pop_back();continue;
                }
                //骑蓝色车的人领先F---》骑蓝色车的人的rank！=4
                if (temp.color=="blue"&&temp.rank==4){
                    result.pop_back();continue;
                }
            }
        }
    }

    //绿色的车子比紫色的车子跑得快
    for (int i = 0; i < result.size(); ++i) {
        if (result[i].color=="green"){
            for (int j = 0; j < result.size(); ++j) {
                //if green.rank>purple.rank then delete these two item
                if (result[j].color=="purple"&&result[i].rank>result[j].rank){
                    if (i<j){
                        result.erase(result.begin()+j);
                        result.erase(result.begin()+i);
                    } else{
                        result.erase(result.begin()+i);
                        result.erase(result.begin()+j);
                    }
                }
            }
        }
    }

    //根据正确结果过滤掉错误组合
    filter(result);

    return result;
}


int main() {
    vector<Combination> res = findResult();
    for (auto & re : res) {
        cout<<"rank:"<<re.rank<<"  ";
        cout<<"name:"<<re.name<<"  " ;
        cout<<"color:"<<re.color<<endl ;
    }
    return 0;
}



